<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

class ActualitesController extends JLControllerBase {

    private $config = null;
    private $path = "Actualites";
    private $title = "Système d'Actualisation CSME > Actualités";
    private $data = null;

    /**
     * Funcion por defecto para los controladores
     * @Retorna No
     */
    public function Index() {
        require JLPATH_MODELS . DS . "SectionModel.php";
        $section = new SectionModel();
        $data['sections'] = $section->getSections();

        require JLPATH_MODELS . DS . "TypeUsersModel.php";
        $typeusers = new TypeUsersModel();
        $data['typeusers'] = $typeusers->getTypeUsers();

        require JLPATH_MODELS . DS . "StateModel.php";
        $states = new StateModel();
        $data['states'] = $states->getStates();

        $data['texttitle'] = " > Actualités";
        $data['classcurrent1'] = ' class="current"';
        $this->View->Show($this->path, $this->title, $data);
    }

    /**
     * Funcion por defecto para los controladores
     * @Retorna No
     */
    public function InsertActualite() {
        $var_ac_title = $_POST['txttitle'];
        $jl = new JLTools();
        $txt_content_actualite = $jl->cleanTextEditor($_POST['txtcontenido']);
        $date = str_replace(array("Fevrier", "Aout", "Decembre"), array("Février", "Août", "Décembre"), $_POST['dat_fecha']);

        $dat_creation_act = $date;
        $dat_modification_act = $date;
        $var_monthyear = str_replace(array("Fevrier", "Aout", "Decembre"), array("Février", "Août", "Décembre"), $_POST['dat_mes']);
        $int_id_user = $_SESSION['_int_id_user'];
        $int_id_section = $_POST['cboseccion'];
        $int_id_state = $_POST['cboestado'];
        $order = '';

        if (!empty($_POST['order'])) {
            $order = serialize(explode(',', $_POST['order']));
        }

        require JLPATH_MODELS . DS . "ActualiteModel.php";
        $act = new ActualiteModel();

        $_b = $act->InsertActualite($var_ac_title, $txt_content_actualite, $dat_creation_act, $dat_modification_act, $int_id_user, $int_id_section, $var_monthyear, $int_id_state, $order);
        if ($_b)
            echo "Données sauvegarder avec succès.";
        else
            echo "Ne peut enregistrer vos données.";
    }

    /**
     * Funcion por defecto para los controladores
     * @Retorna No
     */
    public function UpdateActualite() {
        require JLPATH_MODELS . DS . "ActualiteModel.php";
        $jl = new JLTools();
        $act = new ActualiteModel();

        $_id = $_POST['_id'];
        $var_ac_title = $_POST['txttitle'];
        $txt_content_actualite = $jl->cleanTextEditor($_POST['txtcontenido']);
        $dat_modification_act = $_POST['dat_fecha'];
        $int_id_section = $_POST['cboseccion'];
        $int_id_state = $_POST['cboestado'];
        $order = '';
        if (!empty($_POST['order'])) {
            $order = serialize(explode(',', $_POST['order']));
        }
        echo $act->UpdateActualite($_id, $var_ac_title, $txt_content_actualite, $dat_modification_act, $int_id_section, $int_id_state, $order);
    }

    /**
     * Funcion por defecto para los controladores
     * @Retorna No
     */
    public function DeleteActualite() {
        $_ids = $_POST['_ids'];
        require JLPATH_MODELS . DS . "ActualiteModel.php";
        $act = new ActualiteModel();

        $users = explode("-", $_ids);
        $i = 0;
        foreach ($users as $v) {
            if (trim($v) != "") {
                $val = $act->DeleteActualite(trim($v));
                if (!$val)
                    break;
                $i++;
            }
        }

        $nodelete = (count($users) - $i) - 1;

        if (!$val) {
            echo $i . " donnée(s) effacer! \n$nodelete donnée(s) n'a pu être effacer. Veuillez changer l'état à Provisoire.";
        }
        else
            echo $i . " Donnée(s) effacer!";
    }

    /**
     * Funcion por defecto para los controladores
     * @Retorna No
     */
    public function getActualite() {
        if ($this->Ajax->isAjaxRequest()) {
            $int_id_state = $_POST['_id'][0];

            require JLPATH_MODELS . DS . "ActualiteModel.php";
            $act = new ActualiteModel();

            $actualite = $act->SelectActualite($int_id_state);
            $var_ac_title = $actualite['var_ac_title'];
            $txt_content_actualite = $actualite['txt_content_actualite'];
            $int_id_section = $actualite['int_id_section'];
            $int_id_state = $actualite['int_id_state'];
            $order = NULL;

            if (!empty($actualite['txt_image_galleries'])) {
                $order = unserialize($actualite['txt_image_galleries']);
            }

            $this->Ajax->response(RESPONSE_CODE_OK, array(
                'title' => $var_ac_title,
                'content' => $txt_content_actualite,
                'id_section' => $int_id_section,
                'state' => $int_id_state,
                'order' => $order
            ));
        } else {
            $this->redirect();
        }
    }

    function getNowDateFormat() {
        $config = Config::getInstance();
        //"Lundi 29 Sept. 2009";
        $months = $config->get('months');
        $days = $config->get('days');
        $m = intval(date('n') - 1);
        $Y = intval(date('Y'));
        $d = intval(date('j') - 1);
        $dn = intval(date('N') - 1);

        $datef = $days[$dn] . " " . $d . " " . $months[$m] . " " . $Y;
        return $datef;
    }

}